﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.DataClass;

namespace XJLSC.FM
{
    public partial class CKDJFH : Form
    {
        private readonly DataBase db = new DataBase();
        public delegate void Cdgzzx(object sender, EventArgs e);
        //定义委托对象
        public Cdgzzx cdgzzx;
        public CKDJFH()
        {
            InitializeComponent();
        }

        public delegate void CheckBoxClickedHandler(bool state);
        public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
        {
            bool _bChecked;
            public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
            {
                _bChecked = bChecked;
            }
            public bool Checked
            {
                get { return _bChecked; }
            }
        }
        class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
        {
            Point checkBoxLocation;
            Size checkBoxSize;
            bool _checked = false;
            Point _cellLocation = new Point();
            System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
            public event CheckBoxClickedHandler OnCheckBoxClicked;

            public DatagridViewCheckBoxHeaderCell()
            {
            }

            protected override void Paint(System.Drawing.Graphics graphics,
                System.Drawing.Rectangle clipBounds,
                System.Drawing.Rectangle cellBounds,
                int rowIndex,
                DataGridViewElementStates dataGridViewElementState,
                object value,
                object formattedValue,
                string errorText,
                DataGridViewCellStyle cellStyle,
                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                    dataGridViewElementState, value,
                    formattedValue, errorText, cellStyle,
                    advancedBorderStyle, paintParts);
                Point p = new Point();
                Size s = CheckBoxRenderer.GetGlyphSize(graphics,
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
                p.X = cellBounds.Location.X +
                    (cellBounds.Width / 2) - (s.Width / 2);
                p.Y = cellBounds.Location.Y +
                    (cellBounds.Height / 2) - (s.Height / 2);
                _cellLocation = cellBounds.Location;
                checkBoxLocation = p;
                checkBoxSize = s;
                if (_checked)
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.CheckedNormal;
                else
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.UncheckedNormal;
                CheckBoxRenderer.DrawCheckBox
                (graphics, checkBoxLocation, _cbState);
            }

            protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
            {
                Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
                if (p.X >= checkBoxLocation.X && p.X <=
                    checkBoxLocation.X + checkBoxSize.Width
                && p.Y >= checkBoxLocation.Y && p.Y <=
                    checkBoxLocation.Y + checkBoxSize.Height)
                {
                    _checked = !_checked;
                    if (OnCheckBoxClicked != null)
                    {
                        OnCheckBoxClicked(_checked);
                        this.DataGridView.InvalidateCell(this);
                    }

                }
                base.OnMouseClick(e);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text == "")
                {
                    MessageBox.Show("请选择单据类型！", "系统提示");
                    return;
                }
                string strSql = null;
                if (checkBox1.CheckState == CheckState.Unchecked)
                {
                    if (comboBox1.Text == "车间领料单")
                    {
                        strSql = "SELECT TC005 工作中心,MD002 工作中心名称,TC003 单据日期,TC001 单别,TC002 单号,TC007 备注,MOCTC.CREATOR 录入者,MOCTC.CREATE_DATE 录入日期,TC015 审核者,(CASE WHEN ISNULL(TC022,'N')='' THEN 'N' ELSE ISNULL(TC022,'N') END) AS 复核码 FROM MOCTC LEFT JOIN CMSMD ON TC005=MD001 WHERE TC009='Y' AND (ISNULL(TC022,'N')='N' OR ISNULL(TC022,'N')='') AND TC008='54' AND TC003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TC003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TC002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='')";
                    }
                    else if (comboBox1.Text == "外协领料单")
                    {
                        strSql = "SELECT TC006 供应商,MA002 供应商名称,TC003 单据日期,TC001 单别,TC002 单号,TC007 备注,MOCTC.CREATOR 录入者,MOCTC.CREATE_DATE 录入日期,TC015 审核者,(CASE WHEN ISNULL(TC022,'N')='' THEN 'N' ELSE ISNULL(TC022,'N') END) AS 复核码 FROM MOCTC LEFT JOIN PURMA ON TC006=MA001 WHERE TC009='Y' AND (ISNULL(TC022,'N')='N' OR ISNULL(TC022,'N')='') AND TC008='55' AND TC003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TC003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TC002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='')";
                    }
                    else if (comboBox1.Text == "采购进货单")
                    {
                        strSql = "SELECT TG005 供应商,MA002 供应商名称,TG003 单据日期,TG001 单别,TG002 单号,TG016 备注,PURTG.CREATOR 录入者,PURTG.CREATE_DATE 录入日期,(CASE WHEN ISNULL(TG043,'N')='' THEN 'N' ELSE ISNULL(TG043,'N') END) AS 复核码 FROM PURTG LEFT JOIN PURMA ON TG005=MA001 WHERE TG013='Y' AND (ISNULL(TG043,'N')='N' OR ISNULL(TG043,'N')='') AND TG003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TG003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TG002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='')";
                    }
                    else if (comboBox1.Text == "委外进货单")
                    {
                        strSql = "SELECT TH005 供应商,MA002 供应商名称,TH003 单据日期,TH001 单别,TH002 单号,TH010 备注,MOCTH.CREATOR 录入者,MOCTH.CREATE_DATE 录入日期,(CASE WHEN ISNULL(TH039,'N')='' THEN 'N' ELSE ISNULL(TH039,'N') END) AS 复核码 FROM MOCTH LEFT JOIN PURMA ON TH005=MA001 WHERE TH023='Y' AND (ISNULL(TH039,'N')='N' OR ISNULL(TH039,'N')='') AND TH003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TH003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TH002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='')";
                    }
                    else if (comboBox1.Text == "车间入库单")
                    {
                        strSql = "SELECT TF011 工作中心,MD002 工作中心名称,TF003 单据日期,TF001 单别,TF002 单号,TF005 备注,MOCTF.CREATOR 录入者,MOCTF.CREATE_DATE 录入日期,(CASE WHEN ISNULL(TF017,'N')='' THEN 'N' ELSE ISNULL(TF017,'N') END) AS 复核码 FROM MOCTF LEFT JOIN CMSMD ON TF011=MD001 WHERE TF006='Y' AND (ISNULL(TF017,'N')='N' OR ISNULL(TF017,'N')='') AND TF003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TF003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TF002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='')";
                    }
                    else if (comboBox1.Text == "库存交易单")
                    {
                        strSql = "SELECT TA004 部门编号,ME002 部门名称,TA003 单据日期,TA001 单别,TA002 单号,TA005 备注,INVTA.CREATOR 录入者,INVTA.CREATE_DATE 录入日期,(CASE WHEN ISNULL(TA026,'N')='' THEN 'N' ELSE ISNULL(TA026,'N') END) AS 复核码 FROM INVTA LEFT JOIN CMSME ON TA005=ME001 WHERE TA006='Y' AND (ISNULL(TA026,'N')='N' OR ISNULL(TA026,'N')='') AND TA003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TA003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TA002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND TA009='11'";
                    }
                }
                else if (checkBox1.CheckState == CheckState.Checked)
                {
                    if (comboBox1.Text == "车间领料单")
                    {
                        strSql = "SELECT TC005 工作中心,MD002 工作中心名称,TC003 单据日期,TC001 单别,TC002 单号,TC007 备注,MOCTC.CREATOR 录入者,MOCTC.CREATE_DATE 录入日期,TC015 审核者,(CASE WHEN ISNULL(TC022,'N')='' THEN 'N' ELSE ISNULL(TC022,'N') END) AS 复核码 FROM MOCTC LEFT JOIN CMSMD ON TC005=MD001 WHERE TC009='Y' AND ISNULL(TC022,'N')='Y' AND TC008='54' AND TC003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TC003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TC002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='')";
                    }
                    else if (comboBox1.Text == "外协领料单")
                    {
                        strSql = "SELECT TC006 供应商,MA002 供应商名称,TC003 单据,TC001 单别,TC002 单号,TC007 备注,MOCTC.CREATOR 录入者,MOCTC.CREATE_DATE 录入日期,TC015 审核者,(CASE WHEN ISNULL(TC022,'N')='' THEN 'N' ELSE ISNULL(TC022,'N') END) AS 复核码 FROM MOCTC LEFT JOIN PURMA ON TC006=MA001 WHERE TC009='Y' AND ISNULL(TC022,'N')='Y' AND TC008='55' AND TC003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TC003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TC002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='')";
                    }
                    else if (comboBox1.Text == "采购进货单")
                    {
                        strSql = "SELECT TG005 供应商,MA002 供应商名称,TG003 单据日期,TG001 单别,TG002 单号,TG016 备注,MOCTG.CREATOR 录入者,MOCTG.CREATE_DATE 录入日期,(CASE WHEN ISNULL(TG043,'N')='' THEN 'N' ELSE ISNULL(TG043,'N') END) AS 复核码 FROM PURTG LEFT JOIN PURMA ON TG005=MA001 WHERE TG013='Y' AND ISNULL(TG023,'N')='Y' AND TG003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TG003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TG002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='')";
                    }
                    else if (comboBox1.Text == "委外进货单")
                    {
                        strSql = "SELECT TH005 供应商,MA002 供应商名称,TH003 单据日期,TH001 单别,TH002 单号,TH010 备注,MOCTH.CREATOR 录入者,MOCTH.CREATE_DATE 录入日期,(CASE WHEN ISNULL(TH039,'N')='' THEN 'N' ELSE ISNULL(TH039,'N') END) AS 复核码 FROM MOCTH LEFT JOIN PURMA ON TH005=MA001 WHERE TH023='Y' AND ISNULL(TH039,'N')='Y' AND TH003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TH003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TH002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='')";
                    }
                    else if (comboBox1.Text == "车间入库单")
                    {
                        strSql = "SELECT TF011 工作中心,MD002 工作中心名称,TF003 单据日期,TF001 单别,TF002 单号,TF005 备注,MOCTF.CREATOR 录入者,MOCTF.CREATE_DATE 录入日期,(CASE WHEN ISNULL(TF017,'N')='' THEN 'N' ELSE ISNULL(TF017,'N') END) AS 复核码 FROM MOCTF LEFT JOIN CMSMD ON TF011=MD001 WHERE TF006='Y' AND ISNULL(TF017,'N')='Y' AND TF003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TF003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TF002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='')";
                    }
                    else if (comboBox1.Text == "库存交易单")
                    {
                        strSql = "SELECT TA004 部门编号,ME002 部门名称,TA003 单据日期,TA001 单别,TA002 单号,TA005 备注,INVTA.CREATOR 录入者,INVTA.CREATE_DATE 录入日期,(CASE WHEN ISNULL(TA026,'N')='' THEN 'N' ELSE ISNULL(TA026,'N') END) AS 复核码 FROM INVTA LEFT JOIN CMSME ON TA005=ME001 WHERE TA006='Y' AND ISNULL(TA026,'N')='Y' AND TA003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox2.Text + "'),112) AND TA003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox3.Text + "'),112) AND (TA002='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND TA009='11'";
                    }
                }

                dataGridView1.DataSource = "";
                dataGridView1.Refresh();

                dataGridView1.DataSource = db.GetDataSet1(strSql, "MOCTC").Tables["MOCTC"];
            }
            catch
            {

            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            textBox2.Text = dateTimePicker1.Value.ToShortDateString();
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            textBox3.Text = dateTimePicker2.Value.ToShortDateString();
        }

        private void CKDJFH_Load(object sender, EventArgs e)
        {
            DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
            DatagridViewCheckBoxHeaderCell bbHeader = new DatagridViewCheckBoxHeaderCell();
            colCB.HeaderCell = bbHeader;
            colCB.Name = "colCB";
            colCB.HeaderText = "";
            dataGridView1.Columns.Insert(0, colCB);
            bbHeader.OnCheckBoxClicked +=
                new CheckBoxClickedHandler(abHeader_OnCheckBoxClicked);
            textBox2.Text = DateTime.Now.ToShortDateString();
            textBox3.Text = DateTime.Now.ToShortDateString();
            //DataGridViewCheckBoxColumn colCC = new DataGridViewCheckBoxColumn();
            //DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
            //colCC.HeaderCell = cbHeader;
            //colCC.Name = "colCC";
            //colCC.HeaderText = "";
            //dataGridView2.Columns.Insert(0, colCC);
            //cbHeader.OnCheckBoxClicked +=
            //    new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
 
        }
        private void abHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态
            dataGridView1.EndEdit();
            dataGridView1.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void cbHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态
            dataGridView2.EndEdit();
            dataGridView2.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void dataGridView2_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
           
   
                        string strSql = "";
                        if (comboBox1.Text == "车间领料单")
                        {
                            strSql = "SELECT TE003 序号,TE004 品号,TE017 品名,TE018 规格,CAST(TE005 AS NUMERIC(16,2)) AS 数量,TE008 仓库,A.MC002 仓库名称,TE014 备注 FROM MOCTE LEFT JOIN MOCTC ON TE001=TC001 AND TE002=TC002 LEFT JOIN INVMB ON TE004=MB001 LEFT JOIN CMSMC A ON A.MC001=TE008 WHERE  TE001='" + dataGridView1.CurrentRow.Cells["单别"].Value.ToString() + "' AND TE002='" + dataGridView1.CurrentRow.Cells["单号"].Value.ToString() + "'";
                        }
                        else if (comboBox1.Text == "外协领料单")
                        {
                            strSql = "SELECT TE003 序号,TE004 品号,TE017 品名,TE018 规格,CAST(TE005 AS NUMERIC(16,2)) AS 数量,TE008 仓库,A.MC002 仓库名称,TE014 备注 FROM MOCTE LEFT JOIN MOCTC ON TE001=TC001 AND TE002=TC002 LEFT JOIN INVMB ON TE004=MB001 LEFT JOIN CMSMC A ON A.MC001=TE008 WHERE  TE001='" + dataGridView1.CurrentRow.Cells["单别"].Value.ToString() + "' AND TE002='" + dataGridView1.CurrentRow.Cells["单号"].Value.ToString() + "'";
                        }
                        else if (comboBox1.Text == "采购进货单")
                        {
                            strSql = "SELECT TH003 序号,TH004 品号,TH005 品名,TH006 规格,CAST(TH016 AS NUMERIC(16,2)) AS 数量,TH009 仓库,A.MC002 仓库名称,TH033 备注 FROM PURTH LEFT JOIN PURTG ON TH001=TG001 AND TH002=TG002 LEFT JOIN INVMB ON TH004=MB001 LEFT JOIN CMSMC A ON A.MC001=TH009 WHERE  TH001='" + dataGridView1.CurrentRow.Cells["单别"].Value.ToString() + "' AND TH002='" + dataGridView1.CurrentRow.Cells["单号"].Value.ToString() + "'";
                        }
                        else if (comboBox1.Text == "委外进货单")
                        {
                            strSql = "SELECT TI003 序号,TI004 品号,TI005 品名,TI006 规格,CAST(TI020 AS NUMERIC(16,2)) AS 数量,TI009 仓库,A.MC002 仓库名称,TI040 备注 FROM MOCTI LEFT JOIN MOCTH ON TI001=TH001 AND TI002=TH002 LEFT JOIN INVMB ON TI004=MB001 LEFT JOIN CMSMC A ON A.MC001=TI009 WHERE  TI001='" + dataGridView1.CurrentRow.Cells["单别"].Value.ToString() + "' AND TI002='" + dataGridView1.CurrentRow.Cells["单号"].Value.ToString() + "'";
                        }
                        else if (comboBox1.Text == "车间入库单")
                        {
                            strSql = "SELECT TG003 序号,TG004 品号,TG005 品名,TG006 规格,CAST(TG013 AS NUMERIC(16,2)) AS 数量,TG010 仓库,A.MC002 仓库名称,TG020 备注 FROM MOCTG LEFT JOIN MOCTF ON TG001=TF001 AND TG002=TF002 LEFT JOIN INVMB ON TG004=MB001 LEFT JOIN CMSMC A ON A.MC001=TG010 WHERE  TG001='" + dataGridView1.CurrentRow.Cells["单别"].Value.ToString() + "' AND TG002='" + dataGridView1.CurrentRow.Cells["单号"].Value.ToString() + "'";
                        }
                        else if (comboBox1.Text == "库存交易单")
                        {
                            strSql = "SELECT TB003 序号,TB004 品号,TB005 品名,TB006 规格,CAST(TB007 AS NUMERIC(16,2)) AS 数量,TB012 仓库,A.MC002 仓库名称,TB017 备注 FROM INVTB LEFT JOIN INVTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN INVMB ON TB004=MB001 LEFT JOIN CMSMC A ON A.MC001=TB012 WHERE  TB001='" + dataGridView1.CurrentRow.Cells["单别"].Value.ToString() + "' AND TB002='" + dataGridView1.CurrentRow.Cells["单号"].Value.ToString() + "'";
                        }
                        DataTable dt;
                        dt = db.GetDataSet1(strSql, "MOCTE").Tables["MOCTE"];
                        dataGridView2.DataSource = null;
                        dataGridView2.DataSource = dt;
                        for (int i = 0; i <= dt.Rows.Count - 1; i++)
                        {
                            
                            //int index = this.dataGridView2.Rows.Add();
                            ////MessageBox.Show(index.ToString(),"1");
                            //this.dataGridView2.Rows[index].Cells[0].Value = false;
                            //this.dataGridView2.Rows[index].Cells[1].Value = dt.Rows[i][0].ToString();
                            //this.dataGridView2.Rows[index].Cells[2].Value = dt.Rows[i][1].ToString();
                            //this.dataGridView2.Rows[index].Cells[3].Value = dt.Rows[i][2].ToString();
                            //this.dataGridView2.Rows[index].Cells[4].Value = dt.Rows[i][3].ToString();
                            //this.dataGridView2.Rows[index].Cells[5].Value = dt.Rows[i][4].ToString();
                            //this.dataGridView2.Rows[index].Cells[6].Value = dt.Rows[i][5].ToString();
                            //this.dataGridView2.Rows[index].Cells[7].Value = dt.Rows[i][6].ToString();
                            //this.dataGridView2.Rows[index].Cells[8].Value = dt.Rows[i][7].ToString();
                            //this.dataGridView2.Rows[index].Cells[9].Value = dt.Rows[i][8].ToString();
                            //this.dataGridView2.Rows[index].Cells[10].Value = dt.Rows[i][9].ToString();
                            //this.dataGridView2.Rows[index].Cells[11].Value = dt.Rows[i][10].ToString();
                            //this.dataGridView2.Rows[index].Cells[12].Value = dt.Rows[i][11].ToString();
                            //this.dataGridView2.Rows[index].Cells[13].Value = dt.Rows[i][12].ToString();
                            //this.dataGridView2.Rows[index].Cells[14].Value = dt.Rows[i][13].ToString();
                            //this.dataGridView2.Rows[index].Cells[15].Value = dt.Rows[i][14].ToString();
                            //this.dataGridView2.Rows[index].Cells[16].Value = dt.Rows[i][15].ToString();
                            //this.dataGridView2.Rows[index].Cells[17].Value = dt.Rows[i][16].ToString();
                            //this.dataGridView2.Rows[index].Cells[18].Value = dt.Rows[i][17].ToString();

                        }

                 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strSql = null;
            foreach (DataGridViewRow dr in this.dataGridView1.Rows)
            {

                if (Convert.ToBoolean(dr.Cells["colCB"].Value))
                {
                    if (comboBox1.Text == "车间领料单")
                    {
                        strSql = "UPDATE MOCTC SET TC022='Y' WHERE TC001='" + dr.Cells["单别"].Value + "' AND TC002='" + dr.Cells["单号"].Value + "'";
                    }
                    else if (comboBox1.Text == "外协领料单")
                    {
                        strSql = "UPDATE MOCTC SET TC022='Y' WHERE TC001='" + dr.Cells["单别"].Value + "' AND TC002='" + dr.Cells["单号"].Value + "'";
                    }
                    else if (comboBox1.Text == "采购进货单")
                    {
                        strSql = "UPDATE PURTG SET TG043='Y' WHERE TG001='" + dr.Cells["单别"].Value + "' AND TG002='" + dr.Cells["单号"].Value + "'";
                    }
                    else if (comboBox1.Text == "委外进货单")
                    {
                        strSql = "UPDATE MOCTH SET TH039='Y' WHERE TH001='" + dr.Cells["单别"].Value + "' AND TH002='" + dr.Cells["单号"].Value + "'";
                    }
                    else if (comboBox1.Text == "车间入库单")
                    {
                        strSql = "UPDATE MOCTF SET TF017='Y' WHERE TF001='" + dr.Cells["单别"].Value + "' AND TF002='" + dr.Cells["单号"].Value + "'";
                    }
                    else if (comboBox1.Text == "库存交易单")
                    {
                        strSql = "UPDATE INVTA SET TA026='Y' WHERE TA001='" + dr.Cells["单别"].Value + "' AND TA002='" + dr.Cells["单号"].Value + "'";
                    }

                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                            dr.Cells["复核码"].Value = "Y";
                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                }
            }

            MessageBox.Show("批量复核成功！", "系统提示");
            button1.PerformClick();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string strSql = null;
            foreach (DataGridViewRow dr in this.dataGridView1.Rows)
            {

                if (Convert.ToBoolean(dr.Cells["colCB"].Value))
                {
                    if (comboBox1.Text == "车间领料单")
                    {
                    strSql = "UPDATE MOCTC SET TC022='N' WHERE TC001='" + dr.Cells["单别"].Value + "' AND TC002='" + dr.Cells["单号"].Value + "'";
                    }
                    else if (comboBox1.Text == "外协领料单")
                    {
                        strSql = "UPDATE MOCTC SET TC022='N' WHERE TC001='" + dr.Cells["单别"].Value + "' AND TC002='" + dr.Cells["单号"].Value + "'";
                    }
                    else if (comboBox1.Text == "采购进货单")
                    {
                        strSql = "UPDATE PURTG SET TG043='N' WHERE TG001='" + dr.Cells["单别"].Value + "' AND TG002='" + dr.Cells["单号"].Value + "'";
                    }
                    else if (comboBox1.Text == "委外进货单")
                    {
                        strSql = "UPDATE MOCTH SET TH039='N' WHERE TH001='" + dr.Cells["单别"].Value + "' AND TH002='" + dr.Cells["单号"].Value + "'";
                    }
                    else if (comboBox1.Text == "车间入库单")
                    {
                        strSql = "UPDATE MOCTF SET TF017='N' WHERE TF001='" + dr.Cells["单别"].Value + "' AND TF002='" + dr.Cells["单号"].Value + "'";
                    }
                    else if (comboBox1.Text == "库存交易单")
                    {
                        strSql = "UPDATE INVTA SET TA026='N' WHERE TA001='" + dr.Cells["单别"].Value + "' AND TA002='" + dr.Cells["单号"].Value + "'";
                    }

                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                            dr.Cells["复核码"].Value = "N";
                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                }
            }

            MessageBox.Show("批量取消复核成功！","系统提示");
            button1.PerformClick();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.PerformClick();
        }
    }
}
